﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Code
{
    public partial class StringTools : Form
    {
        public StringTools()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            String source = textBox1.Text;
            textBox2.Text = ToStringBuilder(source);
        }

        string ToStringBuilder(string str)
        {
            String[] Lines = str.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            StringBuilder result = new StringBuilder();
            foreach (string line in Lines)
            {
                result.AppendFormat("sb.AppendLine(\"{0}\");\r\n", line.Replace("\"", "\\\"").TrimEnd());
            }
            return result.ToString();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            string strReg="<[^>]*>";
            string str = textBox3.Text;
            var lst = Regex.Matches(str, strReg);
            foreach (Match c in lst)
            {
                string value = c.Value;
                string newValue ="@"+ value.Substring(1, value.IndexOf(",")-1);
                str= str.Replace(value, newValue);
            }
            textBox4.Text = str;
            
        }

        private void textBox4_DoubleClick(object sender, EventArgs e)
        {
            string strReg = "<[^>]*>";
            string str = textBox3.Text;
            var lst = Regex.Matches(str, strReg);
            foreach (Match c in lst)
            {
                string value = c.Value;
                string newValue = "@" + value.Substring(1, value.IndexOf(",") - 1);
                str = str.Replace(value, newValue);
            }
            string str2 = "";
            foreach (var c in str.Split(new string []{Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries))
            {
                if (string.IsNullOrEmpty(str2))
                {
                    str2 =  c.Trim();
                }
                else
                {
                    str2 += " " + c.Trim();
                }
            }

            textBox4.Text = str2;
        }

        private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode.ToString() == "A")
            {
                textBox3.SelectAll();
            }
        }
    }
}
